package com.yiwei.jmx.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.jmx.MainActivity;
import com.yiwei.jmx.R;
import com.yiwei.jmx.activity.AboutUsActivity;
import com.yiwei.jmx.activity.IdcardIdentityActivity;
import com.yiwei.jmx.activity.LoginTotalActivity;
import com.yiwei.jmx.activity.MyBankCardActivity;
import com.yiwei.jmx.activity.MyBorrowRecordActivity;
import com.yiwei.jmx.activity.MyQuataActivity;
import com.yiwei.jmx.activity.MyRewardActivity;
import com.yiwei.jmx.activity.MyWechatActivity;
import com.yiwei.jmx.activity.PersonalCenterActivity;
import com.yiwei.jmx.activity.SettingActivity;
import com.yiwei.jmx.activity.SuggestActivity;
import com.yiwei.jmx.activity.TransactionRecordActivity;
import com.yiwei.jmx.activity.WechatChatActivity;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.model.UserModel;
import com.yiwei.jmx.constant.UserInfoGlobal;
import com.yiwei.jmx.event.ChangeHomePageEvent;
import com.yiwei.jmx.event.UserEvent;
import com.yiwei.jmx.event.UserUnLoginEvent;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.Spans;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-8-29.
 */

public class MainMyFragment extends BaseFragment {
    @BindView(R.id.btn_notice)
    ImageView btnNotice;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.txt_info)
    TextView txtInfo;
    @BindView(R.id.btn_my_identity)
    LinearLayout btnMyIdentity;
    @BindView(R.id.btn_my_bankcard)
    LinearLayout btnMyBankcard;
    @BindView(R.id.btn_invite)
    LinearLayout btnInvite;
    @BindView(R.id.btn_quota)
    FrameLayout btnQuota;
    @BindView(R.id.btn_my_borrow)
    FrameLayout btnMyBorrow;
    @BindView(R.id.btn_my_reward)
    FrameLayout btnMyReward;
    @BindView(R.id.btn_transaction_record)
    FrameLayout btnTransactionRecord;
    @BindView(R.id.btn_my_extension)
    FrameLayout btnMyExtension;
    @BindView(R.id.btn_chat_center)
    FrameLayout btnChatCenter;
    @BindView(R.id.btn_about_us)
    FrameLayout btnAboutUs;
    @BindView(R.id.btn_setting)
    FrameLayout btnSetting;
    Unbinder unbinder;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.btn_login)
    SuperButton btnLogin;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {


        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                ((MainActivity) getActivity()).getUser();
            }
        });
        EventBus.getDefault().register(this);

        if (UserInfoGlobal.isLogin()) {
            if (UserInfoGlobal.user_info != null) {
                setUserData(UserInfoGlobal.user_info);
            } else {
                ((MainActivity) getActivity()).getUser();
            }
        }
    }

    private void setUserData(UserModel item) {
        btnLogin.setVisibility(View.GONE);
        String info = "";
        if (TextUtils.isEmpty(item.user_name)) {
            info = item.mobile;
        } else {
            info = item.user_name;
        }
        Spanned txt_info = Spans.with(getContext()).font(info, getResources().getDimensionPixelSize(R.dimen.x40), 0xff000000).style(Typeface.BOLD).
                font("\n我的邀请码：" + item.invite_code, getResources().getDimensionPixelSize(R.dimen.x20), 0xff999999).
                build();
        txtInfo.setText(txt_info);
        Glide.with(this).load(item.header).error(R.mipmap.img_default_icon).into(imgIcon);
    }

    public void getClearData() {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        Api.api_service.getClearData().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {

                    ToastUtil.makeText(getContext(), "清楚成功");
                });
    }

    private void clearData() {
        btnLogin.setVisibility(View.VISIBLE);
        txtInfo.setText("");
        UserInfoGlobal.logout();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UserEvent item) {
        setUserData(item.item);
        refresh.setRefreshing(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(UserUnLoginEvent item) {
        clearData();
        refresh.setRefreshing(false);
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (refresh != null) {
            refresh.setRefreshing(false);
        }
    }

    @OnClick({R.id.img_icon, R.id.btn_notice, R.id.btn_wechat, R.id.btn_my_identity, R.id.btn_my_bankcard, R.id.btn_invite, R.id.btn_quota, R.id.btn_my_borrow, R.id.btn_my_reward, R.id.btn_transaction_record, R.id.btn_my_extension, R.id.btn_chat_center, R.id.btn_about_us, R.id.btn_setting})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.img_icon:
                if (UserInfoGlobal.checkLogin()) {
                    Util.startActivity(getContext(), PersonalCenterActivity.class);
                } else {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                }

                break;
            case R.id.btn_notice:
                getClearData();
                break;
            case R.id.btn_wechat:
                Util.startActivity(getContext(), WechatChatActivity.class);
                break;
            case R.id.btn_my_identity:
                EventBus.getDefault().post(new ChangeHomePageEvent(2));
                break;
            case R.id.btn_my_bankcard:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                if (!UserInfoGlobal.getIsIdcardPass()) {
                    Util.startActivity(getContext(), IdcardIdentityActivity.class);
                    return;
                }
                Util.startActivity(getContext(), MyBankCardActivity.class);
                break;
            case R.id.btn_invite:
                EventBus.getDefault().post(new ChangeHomePageEvent(1));
                break;
            case R.id.btn_quota:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                Util.startActivity(getContext(), MyQuataActivity.class);
                break;
            case R.id.btn_my_borrow:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                Util.startActivity(getContext(), MyBorrowRecordActivity.class);
                break;
            case R.id.btn_my_reward:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                Util.startActivity(getContext(), MyRewardActivity.class);
                break;
            case R.id.btn_transaction_record:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                Util.startActivity(getContext(), TransactionRecordActivity.class);
                break;
            case R.id.btn_my_extension:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                Util.startActivity(getContext(), SuggestActivity.class);
                break;
            case R.id.btn_chat_center:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(getContext(), LoginTotalActivity.class);
                    return;
                }
                Util.startActivity(getContext(), MyWechatActivity.class);
                break;
            case R.id.btn_about_us:
                Util.startActivity(getContext(), AboutUsActivity.class);
                break;
            case R.id.btn_setting:
                Util.startActivity(getContext(), SettingActivity.class);
                break;
        }
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        Util.startActivity(getContext(), LoginTotalActivity.class);
    }
}
